package com.myres.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.model.Score;
import com.myres.struts.action.GetMyScoreAction;

import com.myres.struts.action.ScoreService;

public class getMyScores extends ServiceServlet {

	/**
	 * Constructor of the object.
	 */
	public getMyScores() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doRequest(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		int uid=Integer.parseInt(request.getParameter("uid"));
		ScoreService service=new ScoreService();
		List<Score> scores=service.getScoreByUID(uid);
		StringBuilder builder=new StringBuilder();
		builder.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
		builder.append("<response>");
		int total=0;
		builder.append("<scores>");
		int i=0;
		for(Score score:scores){
			i++;
			if(i>15) break;
			builder.append("<score>");
			builder.append("<s_number>");
			total+=score.getScore();
			builder.append(score.getScore());
			builder.append("</s_number>");
			builder.append("<s_time>");
			builder.append(score.getAddTime());
			builder.append("</s_time>");
			builder.append("<s_reason>");
			builder.append(score.getNote());
			builder.append("</s_reason>");
			builder.append("</score>");
		}
		builder.append("</scores>");
		builder.append("<s_total>");
		builder.append(total);
		builder.append("</s_total>");
		builder.append("</response>");
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/xml");
		response.getWriter().write(builder.toString());
		
		
	}
	
	public void init() throws ServletException {
		// Put your code here
	}

}
